<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class='container'>
        <h1>?</h1>
        <section>
            <input type="button" value='start'>
            <input type="button" value='stop'>
        </section>
    </div>


    <script>
        let nameStr = `余秋雨,文章,曾诚,高茂卜,孙建峰,唐明,黄宇,肖玉林,简帮炜,曾薪月,侯芹,田浩民,梁超栋,彭浩峰,李嘉林,房俊霖,马玉琨`;
        let h1 = document.querySelector('h1');
        let stopBtn = document.querySelector("[value='stop']");
        let startBtn = document.querySelector("[value='start']");
        let arr = nameStr.split(',');
        console.log(arr);

        function foo() {
            let i = random(arr.length - 1); // 随机下标
            h1.innerText = arr[i];
        }
        let timer = null;

        // let timer = setInterval(foo, 100);

        // 停止按钮
        stopBtn.onclick = function () {
            clearInterval(timer);
            timer = null;
        }
        // 开始按钮
        startBtn.onclick = function () {
            if (timer) { // null 
                return;
            }
            timer = setInterval(foo, 100);
        }

        // 随机数装封
        function random(min, max) {
            if (!max) {
                [max, min] = [min, 0];
            }
            let number = parseInt(Math.random(min, max) * (max - min + 1) + min);
            return number;
        }
    </script>
</body>

</html>